package com.charles.mybatis;

import com.charles.mybatis.entity.TUser;
import com.charles.mybatis.mapper.TUserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisDemo {

    private SqlSessionFactory sqlSessionFactory;
    @Before
    public void init() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //1.读取mybatis配置文件SqlsessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        inputStream.close();
    }

    @Test
    //测试关联查询
    public void testAutoMapping(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);
        //List<TUser> tUsers = mapper.selectUserJobs1();
        List<TUser> tUsers1 = mapper.selectUserJobs2();
        for (TUser tUser : tUsers1) {
            System.out.println(tUser.getJobs().size());
        }
    }

}
